package com.by.common.crud;

import com.by.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sound.midi.Soundbank;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author 江俊俊
 * @version 1.0
 * @description
 * @date 2025/2/14 16:37
 * @since JDK1.8
 */
public class BaseServiceImpl<T,M extends BaseMapper> implements BaseService<T>{

    @Autowired
    protected M baseMapper;

    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }


    @Override
    public PageBean queryByMap(Map map) {
        int page=1;
        int pageSize=1;
        int total=0;
        if (map.containsKey("page")){
            page=Integer.parseInt(map.get("page").toString());
        }
        if (map.containsKey("pageSize")){
            pageSize=Integer.parseInt(map.get("pageSize").toString());
        }
        PageHelper.startPage(page,pageSize);
        List list = baseMapper.queryByMap(map);

        list.forEach(System.out::println);


        PageInfo pageInfo=new PageInfo(list);
        total=(int) pageInfo.getTotal();
        System.out.println(">>>" + total);

        return new PageBean(page,pageSize,pageInfo.getTotal(),list);
    }
}
